#!/usr/bin/env python
#! _*_ coding:utf-8 _*_
from django.shortcuts import render, render_to_response, redirect
from django.template.context import RequestContext
from django.views.decorators.csrf import csrf_exempt, csrf_protect

# Create your views here.

#排除约束
@csrf_exempt
def login(request):
    print 'view.login'
    if request.method=='POST':
        username = request.POST.get('username',None)
        passwd = request.POST.get('passwd',None)
        if username=='liuhaozzu' and passwd=='123':
            request.session['is_login']=True
            print type(request.session)
            print request.session
            return redirect('/app06/index/',context_instance=RequestContext(request))
        else:
            #return render(request,'app06/login.html',{'msg':'登录失败'},context_instance=RequestContext(request))
            return render_to_response('app06/login.html',{'msg':'post登录失败'},context_instance=RequestContext(request))
    else:
        #return render(request,"app06/login.html",{'msg':'登录失败'},context_instance=RequestContext(request))
        return render_to_response("app06/login.html",{'msg':'get登录失败'},context_instance=RequestContext(request))

@csrf_protect
def index(request):
    is_login=request.session.get('is_login',None)
    print request.session
    if not is_login:
        return render_to_response("app06/login.html")
    else:
        return render_to_response("app06/index.html")

def logout(request):
    #销毁session
    del request.session['is_login']
    return render_to_response("app06/login.html")